package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.jicai.entity.FilterEntity;
import com.sgb.kjwl.model.entity.MachineFilterEntity;
import com.sgb.link.entity.PublicTypeEntity;
import com.sgb.kjwl.model.entity.user.AttrEntity;
import com.sgb.kjwl.model.entity.user.DeviceAttrEntity;
import com.sgb.kjwl.model.entity.user.DeviceTypeEntity;
import com.sgb.kjwl.view.ui.adapter.device.FilterAdapter;
import com.sgb.kjwl.view.ui.adapter.self.publish.DeviceArrAdapter;
import com.sgb.kjwl.view.ui.adapter.self.publish.DevicePublishTypeAdapter;
import com.sgb.kjwl.view.ui.adapter.self.publish.DevicePublishTypeChildAdapter;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MLog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DeviceTypePopWindow extends BasePopWindow {
    private String tag;
    private LinearLayout rootLayout;
    private MaxRecyclerView recyclerView;
    private MaxRecyclerView childView;
    private MaxRecyclerView attView;
    private TextView okView;
    private TextView resetView;
    private FilterResultClick filterResultClick;
    private List<PublicTypeEntity<List<PublicTypeEntity>>> deviceTypeEntityList;
    private List<DeviceTypeEntity<List<DeviceTypeEntity>>> deviceTypeEntityChildList;
    private List<FilterEntity<List<FilterEntity>>> attrEntityList;
    private List<MachineFilterEntity> arrtList;
    private DevicePublishTypeAdapter devicePublishTypeAdapter;
    private DevicePublishTypeChildAdapter devicePublishTypeChildAdapter;
    private DeviceArrAdapter attadapter;
    private FilterAdapter filterAdapter;
    private List<String> noList;
    private Map map;
    private List<DeviceAttrEntity> attributesEntityList;
    //第一级分类编号
    private String pNo = "";
    //第二级类型编号
    private String cNo = "";
    //设备属性
    private String arrNo = "";
    //内容
    private String content = "";
    private String content1 = "";
    private String content2 = "";
    private String content3 = "";

    private boolean isReset = false;

    public interface FilterResultClick {
        void result(String key1, String key2, String key3, Object key4);

        void dimiss();

        void queryChild(String pNo, String cNo, int position);
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }

    public DeviceTypePopWindow(Context context) {
        super(context, DIR_DEFUALT);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_publish_type;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        attributesEntityList = new ArrayList<>();
        rootLayout = holder.getPop().findViewById(R.id.root_layout);
        recyclerView = holder.getPop().findViewById(R.id.filter_list);
        attView = holder.getPop().findViewById(R.id.att_list);
        childView = holder.getPop().findViewById(R.id.child_list);
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (filterResultClick != null) {
                    content = content1 + "-" + content2;
//                    attributesEntityList = attributesEntityList;
                    if (isReset) {
                        isReset = false;
                        filterResultClick.result(pNo, cNo, "", attributesEntityList);
                    } else {
                        filterResultClick.result(pNo, cNo, content, attributesEntityList);
                    }

                }

            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //第一级分类编号
                pNo = "";
                //第二级类型编号
                cNo = "";
                //内容
                content = "";
                content1 = "";
                content2 = "";
                attributesEntityList.clear();
                isReset = true;
                rootLayout.removeAllViews();
                devicePublishTypeAdapter.setDefItem(-1);
                devicePublishTypeChildAdapter.setDefItem(-1);
            }
        });
        devicePublishTypeAdapter = new DevicePublishTypeAdapter(mContext, null);
        devicePublishTypeAdapter.setItemSelect(true);
        devicePublishTypeAdapter.setDefItem(0);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(devicePublishTypeAdapter);
        devicePublishTypeAdapter.setOnItemClick(new AdapterOnItemClick<PublicTypeEntity<List<PublicTypeEntity>>>() {
            @Override
            public void onItemClick(PublicTypeEntity<List<PublicTypeEntity>> deviceTypeEntity, int position) {
                pNo = deviceTypeEntity.getNo();
                content1 = deviceTypeEntity.getName();
                if (deviceTypeEntity.getChildren().size() != 0) {
                    cNo = deviceTypeEntity.getChildren().get(0).getNo();
                    content2 = deviceTypeEntity.getChildren().get(0).getName();
                }
                devicePublishTypeChildAdapter.setDefItem(0);
                devicePublishTypeChildAdapter.setDatas(deviceTypeEntity.getChildren());
            }
        });

        devicePublishTypeChildAdapter = new DevicePublishTypeChildAdapter(mContext, null);
        devicePublishTypeChildAdapter.setItemSelect(true);
        devicePublishTypeChildAdapter.setDefItem(0);
        childView.setLayoutManager(new GridLayoutManager(mContext, 3));
        childView.setAdapter(devicePublishTypeChildAdapter);
        devicePublishTypeChildAdapter.setOnItemClick(new AdapterOnItemClick<PublicTypeEntity>() {
            @Override
            public void onItemClick(PublicTypeEntity deviceTypeEntity, int position) {
                cNo = deviceTypeEntity.getNo();
                content2 = deviceTypeEntity.getName();
                if (filterResultClick != null) {
                    filterResultClick.queryChild(pNo, cNo, 1);
                }
            }
        });

        attadapter = new DeviceArrAdapter(mContext, null);
        attadapter.setItemSelect(true);
        attadapter.setDefItem(-1);
        attView.setLayoutManager(new GridLayoutManager(mContext, 3));
        attView.setAdapter(attadapter);
        attadapter.setOnItemClick(new AdapterOnItemClick<AttrEntity<List<AttrEntity>>>() {
            @Override
            public void onItemClick(AttrEntity<List<AttrEntity>> attrEntity, int position) {
                arrNo = attrEntity.getNo();
                content3 = attrEntity.getName();
            }
        });
    }

    public void setChildView(List<FilterEntity<List<FilterEntity>>> datas) {
        rootLayout.removeAllViews();
        FilterEntity<List<FilterEntity>> entity;
        for (int i = 0; i <= datas.size(); i++) {
            entity = datas.get(i);
            if (entity.getUnit() != null) {
                addTitleView(entity.getName() + "(" + entity.getUnit() + ")");
            } else {
                addTitleView(entity.getName());
            }
            MLog.d("test", "标题 " + entity.getName() + "长度" + datas.size());
            addRecyclerView(entity.getChildren(), i, entity.getType(), entity.getNo());
        }
    }

    private void addTitleView(String title) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        TextView textView = new TextView(mContext);
        textView.setText(title);
        textView.setTextSize(16);
        textView.setTextColor(mContext.getResources().getColor(R.color.color_B6B6B6));
        rootLayout.addView(textView, layoutParams);
    }

    private void addRecyclerView(List<FilterEntity> children, final int i, final int type, String no) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        FilterAdapter adapter = new FilterAdapter(mContext, null, R.layout.item_device_publish_attr);
        DeviceAttrEntity attributesEntity = new DeviceAttrEntity();
        attributesEntity.setAttributeNo(no);
        attributesEntityList.add(attributesEntity);
        adapter.setItemSelect(true);
        adapter.setDefItem(-1);
        MaxRecyclerView recyclerView = new MaxRecyclerView(mContext);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(adapter);
        adapter.setDatas(children);
        adapter.setOnItemClick(new AdapterOnItemClick<FilterEntity>() {
            @Override
            public void onItemClick(FilterEntity filterEntity, int position) {
                MLog.d("test", "child " + filterEntity.getName());
                   /* if (i == 0) {
                        dNo = filterEntity.getNo();
                    } else if (i == 1) {
                        eNo = filterEntity.getNo();
                    }*/
                attributesEntity.setAttributeVal(filterEntity.getStartVal());

            }
        });
        rootLayout.addView(recyclerView, layoutParams);
    }

    public void setData(List<PublicTypeEntity<List<PublicTypeEntity>>> deviceTypeEntityList) {
        this.deviceTypeEntityList = deviceTypeEntityList;
        devicePublishTypeAdapter.setDatas(deviceTypeEntityList);
        devicePublishTypeChildAdapter.setDatas(deviceTypeEntityList.get(0).getChildren());
        pNo = deviceTypeEntityList.get(0).getNo();
        content1 = deviceTypeEntityList.get(0).getName();

        cNo = deviceTypeEntityList.get(0).getChildren().get(0).getNo();
        content2 = deviceTypeEntityList.get(0).getChildren().get(0).getName();
    }

    public void setAttr(List<FilterEntity<List<FilterEntity>>> attrEntityList) {
        this.attrEntityList = attrEntityList;
      /*  if (attrEntityList.size() != 0) {
            attadapter.setDatas(attrEntityList);
        }*/
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (filterResultClick != null) {
            filterResultClick.dimiss();
        }
    }
}
